package com.hzjh.h5mall.ui.wallet

import android.graphics.Paint
import androidx.core.widget.addTextChangedListener
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.R
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.ext.widget.dialog.JHEditDialog
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.style.weight.ToastKt.toastCenter
import com.hzjh.base.utils.function.color
import com.hzjh.base.utils.other.CheckUtils
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.h5mall.databinding.ActivityWithdrawalBinding
import com.hzjh.h5mall.model.wallet.WalletVM

//提现页面
@Route(path = RouteUrl.WITHDRAWAL_ACTIVITY)
class WithdrawalActivity : JHBaseActivity<ActivityWithdrawalBinding,WalletVM>() {
    @JvmField
    @Autowired(name = RouteUrl.PARAMS_BALANCE)
    var mBalance = ""
    @JvmField
    @Autowired(name = RouteUrl.PARAMS_METAMASK_NO)
    var metamastNo = ""

    override fun initData() {
        viewBinding.tvBalanceTip.text = CommUtils.getString(R.string.jh_balance_tip,mBalance)
        viewModel.reqWithdrawNum()
    }

    override fun initView() {
        viewBinding.etMoney.addTextChangedListener {
            try {
                checkInputMoney()
            }catch (e:Exception){
                e.printStackTrace()
            }
        }
        viewBinding.btnWithdrawal.setOnClickListener {
            //metamask 提现申请
            val money=viewBinding.etMoney.text.toString()
            if(CheckUtils.isEmpty(metamastNo)){
                toastCenter(CommUtils.getString(R.string.jh_uninstall_metamask))
                return@setOnClickListener
            }
            if(CheckUtils.isEmpty(money)){
                return@setOnClickListener
            }
            viewModel.reqWithdraw(metamastNo,money)
        }
        viewBinding.tvWithdrawalOther.apply {
            paint.flags = Paint.UNDERLINE_TEXT_FLAG; //下划线
            paint.isAntiAlias = true;//抗锯齿
            setOnClickListener {
                //其他提现方式
                val money=viewBinding.etMoney.text.toString()
                if(CheckUtils.isEmpty(money)){
                    return@setOnClickListener
                }
                JHEditDialog.Builder(this@WithdrawalActivity)
                    .setTile(CommUtils.getString(R.string.jh_Taken))
                    .setHintInput("0xCA07CbD3…A6217AF887")
                    .setBtnTitle(CommUtils.getString(R.string.jh_submit))
                    .setClickSubmit {
                        //提交提现申请
                        viewModel.reqWithdraw(it,money)
                    }
                    .create().show()
            }
        }
        viewBinding.tvAllBalance.setOnClickListener {
            //全部提现
            viewBinding.etMoney.setText(mBalance)
        }
    }

    override fun initObservable() {
        super.initObservable()
        viewModel.withdrawCount.observe(this){
            //当月提现次数
            viewBinding.tvWithdrawalTip.text = CommUtils.getString(R.string.jh_withdrawal_month_tip,"$it")
        }
        viewModel.withdrawResult.observe(this){
            //提现成功
            if(!CheckUtils.isEmpty(it)){
                navigationTo(RouteUrl.WALLET_DETAIL_ACTIVITY){
                    withString(RouteUrl.PARAMS_WALLET_ID,it)
                }
                finish()
            }
        }
    }
    //检测money是否超出
    private fun  checkInputMoney(){
        val money=viewBinding.etMoney.text.toString()
        viewBinding.etMoney.setTextColor(color(com.hzjh.base.style.R.color.coreBlack2))
        viewBinding.tvMoneyTip.setTextColor(color(com.hzjh.base.style.R.color.coreBlack2))
        if(CheckUtils.isEmpty(money)){
           return
        }
        if(money.toDouble()>mBalance.toDouble()){
            viewBinding.etMoney.setTextColor(color(com.hzjh.base.style.R.color.coreRed))
            viewBinding.tvMoneyTip.setTextColor(color(com.hzjh.base.style.R.color.coreRed))
        }
    }

    override fun initViewBinding(): ActivityWithdrawalBinding {
        return ActivityWithdrawalBinding.inflate(layoutInflater)
    }
}